package cn.example.activitytest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class SecondActivity extends BaseActivity {

    private static final String TAG = "SecondActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Log.d(TAG, "onCreate: " + getTaskId());
        Button button2 = findViewById(R.id.button_2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
                startActivity(intent);
            }
        });
        // 接收上一个Activity传递的参数
//        Intent intent = getIntent();
//        Log.d(TAG, "onCreate: extra data is " + intent.getStringExtra("extra_data"));
        // 给上一个Activity返回数据

//        intent.putExtra("extra_data", "hello, first activity");
//        setResult(RESULT_OK,intent);
        // 活动销毁时会把数据传递给上一个活动，调用上一个活动的onActivityResult方法。
        // 可以在上一个活动中重写onActivityResult得到返回的数据
//        finish();
    }
    // 如果用户不是通过点击按钮，而是按下返回键，则可以重写OnBackPrecess()方法
    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("extra_data", "hello, first activity");
        setResult(RESULT_OK,intent);
        finish();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart: " + this.toString());
    }

    public static void actionStart(Context context, String data1, String data2)
    {
        Intent intent = new Intent(context, SecondActivity.class);
        intent.putExtra("data1", data1);
        intent.putExtra("data2", data2);
        context.startActivity(intent);
    }
}